package com.unit.test.case2.service;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;
import com.unit.domain.TestDataContext;
import com.unit.test.case2.config.BaseUnitTestCase;
import com.unit.test.case2.config.MyBatisConfigContextLoader;
import com.unit.test.domain.Member;
import com.unit.test.service.MemberServiceImpl;
import com.unit.util.AssertUtil;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=MyBatisConfigContextLoader.class,inheritInitializers=false)
public class MemberServiceImpl_Test2 extends BaseUnitTestCase {

	@Autowired
	@Qualifier("memberService")
	private MemberServiceImpl memberService;
	
	private TestDataContext memberDataContext;
	
	@PostConstruct
	public void init() throws Throwable {
		if(memberDataContext == null) {
			memberDataContext = testDataManager.loadDataContextByMysql("/csv/member.csv");
			assertConvertManager.prepareColumnToFieldMapFromCsvAndClass(memberDataContext.tableName, memberDataContext.columnNameList, Member.class, null);
		}
	}
	
	@Test
	public void query_test() throws Throwable {
		//准备测试环境：清除相关数据，准备数据
		Long[] memberIds = new Long[] {100L};
		String sql = "delete from member where id = "+memberIds[0];
		queryRunner.update(sql);
		testDataManager.putCsvDataToDb(queryRunner, memberDataContext, memberIds);
		
		//执行业务接口
		Member fromDB = memberService.queryMember(memberIds[0]);	
		
		System.err.println("fromB======"+JSON.toJSONString(fromDB));
		
		//验证校验相关接口数据信息是否正确
		Member fromCsv = assertConvertManager.buildDataObjectFromCsv(memberDataContext.dataBeforeTest.get(memberIds[0]), new Member(), memberDataContext.tableName);
		System.err.println("fromCSV====="+JSON.toJSONString(fromDB));
		Map<String, Object> mapFromCsv = assertConvertManager.convertToAssertMapFromDO(fromCsv); 
		Map<String, Object> mapFromDO = assertConvertManager.convertToAssertMapFromDO(fromDB); 
		AssertUtil.assertMap(mapFromCsv, mapFromDO);
	}
}
